package pl.ptemich.tankwars.states;

import java.util.ArrayList;
import java.util.List;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

import pl.ptemich.tankwars.tools.StateTools;

public class MenuState extends RegisteredState {

  private static final int MENU_ITEM_HEIGHT = 20;
  
  private static final int MENU_ITEM_MARGIN = 5;
  
  private static final int MENU_ITEM_PADDING = 2;
  
  private static final int MENU_ITEM_WIDTH = 100;
  
  private static final int MENU_POS_X = 100;
  
  private static final int MENU_POS_Y = 100;
  
  private static final Color SELECTED_ITEM_COLOR = Color.blue;
  
  private static final Color STANDARD_ITEM_COLOR = Color.white;
  
  private GameContainer container;
  
  private List<MenuPosition> menuPositions = new ArrayList<MenuPosition>();
  
  private MenuPosition activeItem;
  
  private StateBasedGame game;
  
  @Override
  public void init(GameContainer container, StateBasedGame game)
      throws SlickException {
    this.game = game;
    this.container = container;
  }
  
  @Override
  public void update(GameContainer arg0, StateBasedGame arg1, int arg2)
      throws SlickException {
      // do nothing 
  }  

  @Override
  public void render(GameContainer container, StateBasedGame game, Graphics graphics)
      throws SlickException {

      int itemIndex = 0;
      for (MenuPosition item : menuPositions) {
        if (activeItem != null && activeItem == item) {
          graphics.setColor(SELECTED_ITEM_COLOR);
        } else {
          graphics.setColor(STANDARD_ITEM_COLOR);
        }
        graphics.fillRect(MENU_POS_X, 
                          MENU_POS_Y + itemIndex * (MENU_ITEM_HEIGHT + MENU_ITEM_MARGIN), 
                          MENU_POS_X + MENU_ITEM_WIDTH, 
                          MENU_ITEM_HEIGHT);
        graphics.setColor(Color.black);
        graphics.drawString(item.getMenuPositionName(), 
                            MENU_POS_X + MENU_ITEM_PADDING,  
                            MENU_POS_Y + itemIndex * (MENU_ITEM_HEIGHT + MENU_ITEM_MARGIN));
        itemIndex++;
      }    
  }

  public void addPosition(RegisteredState item) {
    menuPositions.add(item);
    if (activeItem == null) {
      activeItem = item;
    }
  }
  
  @Override
  public void keyPressed(int key, char c) {    
    super.keyPressed(key, c);
    if (activeItem != null) {
      int index = menuPositions.indexOf(activeItem);
      switch (key) {
      case Input.KEY_UP:
        if (index > 0) {
          activeItem = menuPositions.get(index - 1);
        } else {
          activeItem = menuPositions.get(menuPositions.size() - 1);
        }
        break;
      case Input.KEY_DOWN:
        if (index < menuPositions.size() - 1) {
          activeItem = menuPositions.get(index + 1);
        } else {
          activeItem = menuPositions.get(0);
        }        
        break;
      case Input.KEY_ENTER:
        executeCommand();
        break;     
      case Input.KEY_ESCAPE:
        container.exit();
        break;
      }
    }
  } 
  
  
  public void executeCommand() {
    StateTools.stateTransition(activeItem.getStateTransition());
  }

}
